package com.ruoyi.web.controller.userheath.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.controller.userheath.entity.UserHealth;
import com.ruoyi.web.controller.userheath.mapper.UserHealthMapper;
import com.ruoyi.web.controller.userheath.service.UserHealthService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class UserHealthServiceImpl extends ServiceImpl<UserHealthMapper, UserHealth> implements UserHealthService {

    @Override
    public boolean recordSteps(Long userId, Integer steps) {
        UserHealth health = this.getHealthDataByDate(userId, LocalDate.now());
        if (health == null) {
            health = new UserHealth();
            health.setUserId(userId);
            health.setSteps(steps);
            health.setRecordDate(LocalDate.now());
            health.setCreatedAt(LocalDateTime.now());
        } else {
            health.setSteps(steps);
        }
        health.setUpdatedAt(LocalDateTime.now());
        return this.saveOrUpdate(health);
    }

    @Override
    public boolean updateWeight(Long userId, Double weight) {
        UserHealth health = this.getHealthDataByDate(userId, LocalDate.now());
        if (health == null) {
            health = new UserHealth();
            health.setUserId(userId);
            health.setWeight(weight);
            health.setRecordDate(LocalDate.now());
            health.setCreatedAt(LocalDateTime.now());
        } else {
            health.setWeight(weight);
        }
        health.setUpdatedAt(LocalDateTime.now());
        return this.saveOrUpdate(health);
    }

    @Override
    public boolean recordCalories(Long userId, Double calories) {
        UserHealth health = this.getHealthDataByDate(userId, LocalDate.now());
        if (health == null) {
            health = new UserHealth();
            health.setUserId(userId);
            health.setCaloriesBurned(calories);
            health.setRecordDate(LocalDate.now());
            health.setCreatedAt(LocalDateTime.now());
        } else {
            health.setCaloriesBurned(calories);
        }
        health.setUpdatedAt(LocalDateTime.now());
        return this.saveOrUpdate(health);
    }

    @Override
    public UserHealth getHealthDataByDate(Long userId, Date date) {
        return this.getOne(new QueryWrapper<UserHealth>()
                .eq("user_id", userId)
                .eq("record_date", date));
    }

    public UserHealth getHealthDataByDate(Long userId, LocalDate date) {
        return this.getOne(new QueryWrapper<UserHealth>()
                .eq("user_id", userId)
                .eq("record_date", date));
    }

    @Override
    public List<UserHealth> getAllHealthData(Long userId) {
        return this.list(new QueryWrapper<UserHealth>().eq("user_id", userId));
    }

    public Long getAllPlanCount(Long userId) {
        LambdaQueryWrapper<UserHealth> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserHealth::getUserId, userId);
        return baseMapper.selectCount(wrapper);
    }
}
